﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using Sunny.UI;
using VirtualHost.Models;
using VirtualHost.Services;

namespace VirtualHost.WinForm {
    public partial class FrmMain : WinFormBase {
        public FrmMain() {
            InitializeComponent();
            dgvMain.AutoGenerateColumns = false;
            Initialize();
        }
        HostService service = new HostService();
        NginxConfigService nginxConfigService = null;
        private void Initialize() {
            if (service.IsRuning()) {
                btnStart.Text = "停止";
                btnStart.TagString = "stop";
                btnStart.Symbol = 93;
            }

            String file_name = $@"{service.Settimgs.NginxPath}\conf\nginx.conf";
            nginxConfigService = new NginxConfigService(file_name);
            LoadData();
        }

        #region 1.0 加载刷新数据
        public void LoadData() {
            String file_name = $@"{service.Settimgs.NginxPath}\conf\nginx.conf";
            List<Models.HostSiteEntity> list = nginxConfigService.ParseConfigFile( );
            dgvMain.DataSource = list;
        }
        #endregion

        #region 1.0 显示状态消息 +void ShowMessage(String format, params Object[] args)
        /// <summary>
        /// 显示状态消息
        /// </summary>
        /// <param name="format"></param>
        /// <param name="args"></param>
        public void ShowMessage(String format, params Object[] args) {
            String msg = String.Format(format, args);
            UpdateWinUI(() => {
                lblTipMsg.Text = msg;
            });
        }
        #endregion

        #region 2.0 启动服务
        private void btnStart_Click(object sender, EventArgs e) {
            UISymbolButton button = sender as UISymbolButton;
            button.Enabled = false;
            if (button.TagString == "start") {
                Thread thread = new Thread(() => {
                    service.Start();
                    UpdateWinUI(() => {
                        button.Enabled = true;
                        ShowMessage("service is start!");
                        btnStart.Text = "停止";
                        button.TagString = "stop";
                        btnStart.Symbol = 93;
                    });
                });
                thread.IsBackground = true;
                thread.Start();
            } else {
                Thread thread = new Thread(() => {
                    service.Stop();
                    UpdateWinUI(() => {
                        button.Enabled = true;
                        btnStart.Text = "启动";
                        button.TagString = "start";
                        btnStart.Symbol = 73;
                        ShowMessage("service is stop!");
                    });
                });
                thread.IsBackground = true;
                thread.Start();
            }
        }

        #endregion

        #region 3.0 重启服务
        private void btnRestart_Click(object sender, EventArgs e) {
            UISymbolButton button = sender as UISymbolButton;
            button.Enabled = false;
            Thread thread = new Thread(() => {
                service.Stop();
                Thread.Sleep(2000);
                service.Start();
                this.UpdateWinUI(() => {
                    button.Enabled = true;
                    ShowMessage("service is restarted!");
                });
            });
            thread.IsBackground = true;
            thread.Start();
        }



        #endregion

        #region 4.0 打开Nginx工作目录
        private void btnSiteFolder_Click(object sender, EventArgs e) {

            Process.Start("explorer", service.Settimgs.NginxPath);

        }


        #endregion

        #region 5.0 新增站点
        private void btnAddSite_Click(object sender, EventArgs e) {
            FrmSiteEdit frm = new FrmSiteEdit(nginxConfigService);
            if (frm.Create() == DialogResult.OK) {
                LoadData();
            }
        }
        #endregion

        #region 6.0 双击编辑站点
        private void dgvMain_CellDoubleClick(object sender, DataGridViewCellEventArgs e) {
            if (e.RowIndex == -1) {
                return;
            }
            FrmSiteEdit frm = new FrmSiteEdit(nginxConfigService);
            UIDataGridView gridView = sender as UIDataGridView;
            HostSiteEntity entity= nginxConfigService.HostSites[e.RowIndex];
            String id = gridView.Rows[e.RowIndex].Cells[0].Value.ToString();
            if (frm.Modify(entity) == DialogResult.OK) {
                LoadData();
            }
        }
        #endregion


        #region 7.0 刷新数据
        private void btnRefresh_Click(object sender, EventArgs e) {
            LoadData();
        }
        #endregion

        #region 8.0 打开地址
        private void btnOpensiteUrl_Click(object sender, EventArgs e) {
            DataGridViewSelectedRowCollection rows = dgvMain.SelectedRows;
            if (rows.Count == 0) {
                return;
            }
            String url = rows[0].Cells["ColumnDomain"].Value.ToString();
            Process.Start("explorer", String.Format("http{0}://{1}","", url));

        }
        #endregion

        #region 9.0 Hosts文件
        private void btnOpenHostFile_Click(object sender, EventArgs e) {
            FrmEditHosts frm = new FrmEditHosts();
            frm.ShowDialog();
        }
        #endregion

        #region 10.0 打开系统配置
        private void btnSysSettings_Click(object sender, EventArgs e) {
            FrmSettings frm = new FrmSettings(service);
            frm.ShowDialog();

        } 
        #endregion



    }
}
